package com.showde.shoppingmall.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.domain.ShoppingCart;

public class ShoppingcartAdapter extends BaseAdapter {
	

	private List<ShoppingCart> shoppingCarts;
	private Context context;
	ImageLoader imageLoader;
	
	private List<String> productChecked = new ArrayList<String>(); 

	public ShoppingcartAdapter(Context context, List<ShoppingCart> shoppingCarts) {
		this.context = context;
		this.shoppingCarts = shoppingCarts;
		imageLoader = new ImageLoader(context);
	}

	public void resetData(List<ShoppingCart> shoppingCarts) {
		this.shoppingCarts = shoppingCarts;
	}
	
	public ArrayList<String> getChechedProductIds(){
		return (ArrayList<String>) this.productChecked;
	}

	@Override
	public int getCount() {
		return shoppingCarts.size();
	}

	@Override
	public ShoppingCart getItem(int position) {
		return shoppingCarts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_shoppingcart_pro, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.txt_pro_desc);
			holder.count = (TextView) convertView.findViewById(R.id.txt_pro_count);
			holder.price = (TextView) convertView.findViewById(R.id.txt_pro_price);
			holder.logo = (ImageView) convertView.findViewById(R.id.img_product_icon);
			holder.checked = (CheckBox) convertView.findViewById(R.id.cb_ischeck);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		ShoppingCart shoppingCart = getItem(position);
		final String productId = shoppingCart.getProductId();
		holder.title.setText(shoppingCart.getProductTitle());
		holder.count.setText(String.valueOf("数量："+shoppingCart.getProductCount()));
		holder.price.setText(String.valueOf("单价：￥"+shoppingCart.getProductPrice()));
		imageLoader.displayImage(shoppingCart.getProductLogo(), holder.logo);
		
		holder.checked.setChecked(productChecked.contains(shoppingCart.getProductId()));
			
		holder.checked.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					productChecked.add(productId);
				}else{
					productChecked.remove(productId);
				}
			}
		});
		
		return convertView;
	}

	private final class ViewHolder {
		TextView title;
		TextView count;
		TextView price;
		ImageView logo;
		CheckBox checked;
	}

}
